package jh.com.jhappbf.unload;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONObject;

import java.util.ArrayList;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.ui.State;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author : wwh
 * e-mail  : 953889276@qq.com
 * time    : 2019-5-20 15:22:47
 * desc    :
 */
public class BuyConfirm extends AbActivity {


    @BindView(R.id.buy_card_id)
    EditText buyCardId;
    @BindView(R.id.buy_query)
    Button buyQuery;
    @BindView(R.id.buy_card_info)
    TextView buyCardInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.activity_buy_confirm);
        ButterKnife.bind(this);
        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("采购确认");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        View rightViewMore = mInflater.inflate(R.layout.title_right_ok_btn_view, null);
        mAbTitleBar.addRightView(rightViewMore);
        Button mTitleRightBtn = rightViewMore.findViewById(R.id.title_right_ok_btn);
        mTitleRightBtn.setOnClickListener(v -> buyConfirm());
    }

    private void buyConfirm() {
        String methodName = "buyConfirm";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        param.add("cardId");
        String cardId = buyCardId.getText().toString().trim();
        if (cardId.length() > 0) {
            value.add(cardId);
        } else {
            Toast.makeText(this, "请输入卡号信息", Toast.LENGTH_SHORT).show();
            return;
        }
        String cardInfo = buyCardInfo.getText().toString().trim();
        if (cardInfo.length() < 10) {
            Toast.makeText(this, "请先查询卡号信息", Toast.LENGTH_SHORT).show();
        }
        param.add("confirmor");
        value.add(State.mName);
        Listener<String> listener = response -> {
            JSONObject jsonObject = JsonHelper.getJsonObject(response);
            if (jsonObject != null) {
                String result = jsonObject.optString("result");
                String memo = jsonObject.optString("memo");
                if ("true".equals(result)) {
                    Toast.makeText(BuyConfirm.this, memo, Toast.LENGTH_SHORT).show();
                    refresh();
                } else {
                    Toast.makeText(BuyConfirm.this, memo, Toast.LENGTH_SHORT).show();
                }
            }
        };
        ErrorListener errorListener = volleyError -> Toast.makeText(BuyConfirm.this, "网络异常", Toast.LENGTH_SHORT).show();
        VolleyCallWebService webService = new VolleyCallWebService(methodName, param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(this).addRequest(webService);
    }

    private void refresh() {
        buyCardId.setText("");
        buyCardInfo.setText("");
    }

    @OnClick(R.id.buy_query)
    public void onViewClicked() {
        String cardId = buyCardId.getText().toString().trim();
        if (cardId.length() > 0) {
            getCardInfo(cardId);
        } else {
            Toast.makeText(this, "请输入卡号", Toast.LENGTH_SHORT).show();
        }

    }

    private void getCardInfo(String cardId) {
        String methodName = "getCardInfo";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        param.add("cardId");
        value.add(cardId);
        param.add("type");
        value.add("buy");
        Listener<String> listener = response -> {
            JSONObject jsonObject = JsonHelper.getJsonObject(response);
            if (jsonObject != null) {
                String memo = jsonObject.optString("memo");
                if (memo.length() > 0) {
                    Toast.makeText(BuyConfirm.this, memo, Toast.LENGTH_SHORT).show();
                    return;
                }
                String cargoname = jsonObject.optString("Cargoname");
                String platenum = jsonObject.optString("Platenum");
                String driver = jsonObject.optString("Driver");
                String driverPhone = jsonObject.optString("DriverPhone");
                String spec = jsonObject.optString("Specification");
                String sender = jsonObject.optString("Sender");
                String recevier = jsonObject.optString("Recevier");
                String cardType = jsonObject.optString("cardType");

                String info = ("车号: " + platenum + "\n") +
                        "司机: " + driver + "\n" +
                        "电话: " + driverPhone + "\n" +
                        "货名: " + cargoname + "\n" +
                        "规格: " + spec + "\n" +
                        "发货: " + sender + "\n" +
                        "收货: " + recevier + "\n" +
                        "卡类型 :" + cardType + "\n";
                buyCardInfo.setText(info);
            } else {
                Toast.makeText(BuyConfirm.this, "网络请求异常", Toast.LENGTH_SHORT).show();
            }
        };
        ErrorListener errorListener = volleyError -> Toast.makeText(BuyConfirm.this, "网络异常", Toast.LENGTH_SHORT).show();
        VolleyCallWebService webService = new VolleyCallWebService(methodName, param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(this).addRequest(webService);
    }
}
